<?
/*
%%%copyright%%%
 * phpMyTicket - ticket reservation system
 * Copyright (C) 2004-2005 Anna Putrino, Stanislav Chachkov. All rights reserved.
 *
 * This file is part of phpMyTicket.
 *
 * This file may be distributed and/or modified under the terms of the
 * "GNU General Public License" version 2 as published by the Free
 * Software Foundation and appearing in the file LICENSE included in
 * the packaging of this file.
 *
 * Licencees holding a valid "phpmyticket professional licence" version 1
 * may use this file in accordance with the "phpmyticket professional licence"
 * version 1 Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 *
 * The "phpmyticket professional licence" version 1 is available at
 * http://www.phpmyticket.com/ and in the file
 * PROFESSIONAL_LICENCE included in the packaging of this file.
 * For pricing of this licence please contact us via e-mail to 
 * info@phpmyticket.com.
 * Further contact information is available at http://www.phpmyticket.com/
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
 *
 * Contact info@phpmyticket.com if any conditions of this licencing isn't 
 * clear to you.
 
 */
?>
<?

require_once("classes/ShopDB.php");
require_once("admin/AdminView.php");
require_once("page_classes/CountriesList.php");
require_once('classes/Ort.php');

class OrtView extends AdminView{

function pm_list ($ort_id){
  $query="SELECT * FROM PlaceMap2 WHERE pm_ort_id='$ort_id'";
  if(!$res=ShopDB::query($query)){return;}
  
  $alt=0;
  echo "<table class='admin_list' width='500' cellspacing='0' cellpadding='4'>\n";
  echo "<tr><td class='admin_list_title' colspan='7' align='center'>".pm_title."</td></tr>\n";

  while($row=mysql_fetch_assoc($res)){
    echo "<tr class='admin_list_row_$alt'>";
    if($row['pm_lock']){
      echo "<td class='admin_list_item'><img src='images/lock.png' alt='".locked."' title='".locked."'></td>";
    }else{
      echo "<td class='admin_list_item'></td>";
    }
    echo "<td class='admin_list_item'>{$row['pm_id']}</td>\n";
    echo "<td class='admin_list_item' width='80%'>{$row['pm_name']}</td>\n";
    echo "<td class='admin_list_item'><a class='link' href='{$_SERVER['PHP_SELF']}?action=view&pm_id={$row['pm_id']}&pm_ort_id=$ort_id'><img src='images/view.png' border='0' alt='".view."' title='".view."'></a></td>\n";
    if($row['pm_lock']){
      echo "<td class='admin_list_item'><a class='link' href='{$_SERVER['PHP_SELF']}?action=unlock&pm_id={$row['pm_id']}&pm_ort_id=$ort_id'><img src='images/unlock.png' border='0' alt='".unlock."' title='".unlock."'></a></td>\n";
      echo "<td class='admin_list_item'></td>\n";
    }else{
      echo "<td class='admin_list_item'><a class='link' href='{$_SERVER['PHP_SELF']}?action=edit&pm_id={$row['pm_id']}&pm_ort_id=$ort_id'><img src='images/edit.gif' border='0' alt='".edit."' title='".edit."'></a></td>\n";
      echo "<td class='admin_list_item'><a class='link' href='javascript:if(confirm(\"".delete_item."\")){location.href=\"{$_SERVER['PHP_SELF']}?action=remove&pm_id={$row['pm_id']}&pm_ort_id=$ort_id\";}'><img src='images/trash.png' border='0' alt='".remove."' title='".remove."'></a></td>\n";
    }
    echo "<td class='admin_list_item'><a class='link' href='{$_SERVER['PHP_SELF']}?action=copy&pm_id={$row['pm_id']}&pm_ort_id=$ort_id'><img src='images/copy.png' border='0' alt='".copy."' title='".copy."'></a></td>\n";
    echo "</tr>";
    $alt=($alt+1)%2;
  }
  echo "</table>\n";
  
  echo "<br><center><a class='link' href='{$_SERVER['PHP_SELF']}?action=add&pm_ort_id=$ort_id'>".add."</a></center>";
}


function ort_view (&$data){
  global $_SHOP;

  $country=new CountriesList();
  $data["ort_country_name"]=$country->getCountry($data["ort_country"]);

  echo "<table class='admin_form' width='500' cellspacing='1' cellpadding='4'>\n";
  echo "<tr><td class='admin_list_title' colspan='2'>".$data["ort_name"]."</td></tr>";   
  $this->print_field('ort_id',$data);
  $this->print_field('ort_name',$data);
  $this->print_field_o('ort_address',$data);
  $this->print_field_o('ort_address1',$data);	 
  $this->print_field_o('ort_zip',$data);
  $this->print_field_o('ort_city',$data);
  $this->print_field_o('ort_country_name',$data);
  $this->print_field_o('ort_phone',$data);
  $this->print_field_o('ort_fax',$data);
  //$this->print_field('ort_plan_nr',$data);
  $this->print_field_o('ort_url',$data);
  $this->view_file('ort_image',$data,$err);

  if($data['ort_organizer_id']==$_SHOP->organizer_id or !$data['ort_organizer_id']){
    require_once('admin/PlaceMapView2.php');
    PlaceMapView2::pm_list($data['ort_id']);
  }
  
  if($data['ort_organizer_id']!=$_SHOP->organizer_id){
    echo "<br><center><a class='link' href='{$_SERVER['PHP_SELF']}?action=import'>".admin_list."</a></center>";
  }else{
    echo "<br><center><a class='link' href='{$_SERVER['PHP_SELF']}'>".admin_list."</a></center>";
  }
}

function ort_form (&$data, &$err,$title){
  global $_SHOP;

  echo "<form method='POST' action='{$_SERVER['PHP_SELF']}' enctype='multipart/form-data'>\n";
  echo "<table class='admin_form' width='500' cellspacing='1' cellpadding='4'>\n";
  echo "<tr><td class='admin_list_title' colspan='2'>".$title."</td></tr>"; 
  $this->print_field_o('ort_id',$data, $err);
  $this->print_input('ort_name',$data, $err,25,100);
  $this->print_input('ort_address',$data, $err,25,75);
  $this->print_input('ort_address1',$data, $err,25,75);	 
  $this->print_input('ort_zip',$data, $err,10,20);
  $this->print_input('ort_city',$data, $err,25,50);
  echo "<tr><td class='admin_name'>".ort_country."</td><td class='admin_value'>";
  $countries=new CountriesList();
  $countries->printForm('ort_country',$data['ort_country'],$err);
  echo "</td></tr>";
  $this->print_input('ort_phone',$data, $err,25,50);
  $this->print_input('ort_fax',$data, $err,25,50);
  //$this->print_input('ort_plan_nr',$data, $err,6,100);
  $this->print_input('ort_url',$data, $err,30,100);
  $this->print_file('ort_image',$data,$err);    

  if($data['ort_id']){
    echo "<input type='hidden' name='ort_id' value='{$data['ort_id']}'/>\n";
    echo "<input type='hidden' name='action' value='update'/>\n";
  }else{
    echo "<input type='hidden' name='action' value='insert'/>\n";
  }
  
  echo "<tr><td align='center' class='admin_value' colspan='2'>
  <input type='submit' name='submit' value='".save."'>
  <input type='reset' name='reset' value='".res."'></td></tr>";
  echo "</table>\n";

  echo "<br><center><a class='link' href='{$_SERVER['PHP_SELF']}'>".admin_list."</a></center>";
}

function ort_check (&$data, &$err){

  if(empty($data['ort_name'])){$err['ort_name']=mandatory;}
  if(empty($data['ort_city'])){$err['ort_city']=mandatory;}

  
  return empty($err);
}

function ort_list (){
  global $_SHOP;
 
  $query="select * from Ort where ort_organizer_id={$_SHOP->organizer_id}";
  if(!$res=ShopDB::query($query)){
    return;
  }


  $alt=0;
 echo "<table class='admin_list' width='500' cellpadding='5' cellspacing='0'>\n";
 echo "<tr><td class='admin_list_title' colspan='5' align='center'>".ort_title."</td></tr>\n";

 while($row=mysql_fetch_assoc($res)){
     echo "<tr class='admin_list_row_$alt'>";
     echo "<td class='admin_list_item'>{$row['ort_id']}</td>\n";
     echo "<td class='admin_list_item' width='80%'>{$row['ort_name']}</td>\n";
     echo "<td class='admin_list_item'><a class='link' href='view_ort.php?action=view&ort_id={$row['ort_id']}'><img src='images/view.png' border='0' alt='".view."' title='".view."'></a></td>\n";
     echo "<td class='admin_list_item'><a class='link' href='view_ort.php?action=edit&ort_id={$row['ort_id']}'><img src='images/edit.gif' border='0' alt='".edit."' title='".edit."'></a></td>\n";
     echo "<td class='admin_list_item'><a class='link' href='javascript:if(confirm(\"".delete_item."\")){location.href=\"view_ort.php?action=remove&ort_id={$row['ort_id']}\";}'><img src='images/trash.png' border='0' alt='".remove."' title='".remove."'></a></td>\n";
     echo "</tr>";
     $alt=($alt+1)%2;
  } 
  echo "</table>\n";  
  echo "<br><center><a class='link' href='{$_SERVER['PHP_SELF']}?action=add'>".add."</a></center>";
//  echo "<br><center><a class='link' href='{$_SERVER['PHP_SELF']}?action=import'>".import."</a></center>";
}

function shared_ort_list (){

  $query="select * from Ort WHERE ort_organizer_id=0";
  if(!$res=ShopDB::query($query)){
    user_error(mysql_error());
    return;
  }

  $alt=0;
  echo "<table class='admin_list' width='500' cellpadding='5' cellspacing='0'>\n";
  echo "<tr><td class='admin_list_title' colspan='4' align='center'>".shared_ort_title."</td></tr>\n";

  while($row=mysql_fetch_assoc($res)){
     echo "<tr class='admin_list_row_$alt'>";
     echo "<td class='admin_list_item'>{$row['ort_id']}</td>\n";
     echo "<td class='admin_list_item' width='60%'>{$row['ort_name']}</td>\n";
     echo "<td class='admin_list_item'>{$row['ort_country']}{$row['ort_zip']} {$row['ort_city']}</td>\n";
     echo "<td class='admin_list_item'><a class='link' href='view_ort.php?action=view&ort_id={$row['ort_id']}'><img src='images/view.png' border='0' alt='".view."' title='".view."'></a></td>\n";
     echo "<td class='admin_list_item'><a class='link' href='view_ort.php?action=copy&ort_id={$row['ort_id']}'><img src='images/copy.png' border='0' alt='".copy."' title='".copy."'></a></td>\n";
     echo "</tr>";
     $alt=($alt+1)%2;
  }
  
  echo "</table>\n";
}


function photo_post ($data, $ort_id){
  return $this->file_post($data,$ort_id, 'Ort', 'ort', '_image');
}



function draw () { 
global $_SHOP;

if(preg_match('/_pm$/',$_GET['action']) or preg_match('/_pm$/',$_POST['action'])){
      require_once('admin/PlaceMapView2.php');
      $pm_view=new PlaceMapView2;  
      if($pm_view->draw()){
        if($ort_id=$_POST['pm_ort_id'] or $ort_id=$_GET['pm_ort_id']){
          $query="SELECT * FROM Ort WHERE ort_id='$ort_id' and (ort_organizer_id=$_SHOP->organizer_id)";
          if(!$row=ShopDB::query_one_row($query)){
            return 0;
          }
          $this->ort_view($row);
	} 
      }

}
elseif($_GET['action']=='copy' and $_GET['ort_id']){
  
  $ort=Ort::load($_GET['ort_id']);
  $ort->copy($_SHOP->organizer_id);

  $this->ort_list();
}else

if($_POST['action']=='insert'){
  if(!$this->ort_check($_POST,$err)){
    $this->ort_form($_POST,$err,ort_add_title);
  }else{
    $query="INSERT Ort (ort_name,ort_address,ort_address1,
            ort_zip,ort_city,ort_country,ort_phone,ort_fax,
	    ort_url,ort_organizer_id) VALUES 
	    ('".$this->q($_POST['ort_name'])."',
	    '".$this->q($_POST['ort_address'])."',
	    '".$this->q($_POST['ort_address1'])."',
	    '".$this->q($_POST['ort_zip'])."',
	    '".$this->q($_POST['ort_city'])."',
	    '".$this->q($_POST['ort_country'])."',
	    '".$this->q($_POST['ort_phone'])."',
	    '".$this->q($_POST['ort_fax'])."',
	    '".$this->q($_POST['ort_url'])."',
	    {$_SHOP->organizer_id}
	    )";
    if(!ShopDB::query($query)){
      return 0;
    }
    
    $ort_id=mysql_insert_id();
    
    if(!$this->photo_post($_POST,$ort_id)){
      echo "<div class=error>".img_loading_problem."<div>";
    }


    $this->ort_list();
  }
}else
if($_POST['action']=='update'){
  if(!$this->ort_check($_POST,$err)){
    $this->ort_form($_POST,$err,ort_update_title);
  }else{
    $query="UPDATE Ort SET ort_name='".$this->q($_POST['ort_name'])."',
            ort_address='".$this->q($_POST['ort_address'])."',
	    ort_address1='".$this->q($_POST['ort_address1'])."',
	    ort_zip='".$this->q($_POST['ort_zip'])."',
	    ort_city='".$this->q($_POST['ort_city'])."',
	    ort_country='".$this->q($_POST['ort_country'])."',
	    ort_phone='".$this->q($_POST['ort_phone'])."',
	    ort_fax='".$this->q($_POST['ort_fax'])."',
	    ort_plan_nr='".$this->q($_POST['ort_plan_nr'])."',ort_url='".$this->q($_POST['ort_url'])."' 
	    WHERE ort_id='".$this->q($_POST['ort_id'])."' and
	    ort_organizer_id={$_SHOP->organizer_id}";
    if(!ShopDB::query($query)){
      return 0;
    }

    if(!$this->photo_post($_POST,$_POST['ort_id'])){
      echo "<div class=error>".img_loading_problem."<div>";
    }

    $this->ort_list();
  }
}else
if($_GET['action']=='add'){
  $this->ort_form($row,$err,ort_add_title);
}else 
if($_GET['action']=='edit'){
  $query="SELECT * FROM Ort WHERE ort_id='{$_GET['ort_id']}' and ort_organizer_id=$_SHOP->organizer_id";
  if(!$row=ShopDB::query_one_row($query)){
    user_error(mysql_error());
    return 0;
  }
  $this->ort_form($row,$err,ort_update_title);
}else 
if($_GET['action']=='view'){
  $query="SELECT * FROM Ort WHERE ort_id='{$_GET['ort_id']}' and (ort_organizer_id=$_SHOP->organizer_id or ort_organizer_id=0)";
  if(!$row=ShopDB::query_one_row($query)){
    return 0;
  }
  $this->ort_view($row);
}else 
if($_GET['action']=='remove' and $_GET['ort_id']>0){

  //check if not in use
  $query="SELECT ort_name FROM Ort,Event Where ort_id='{$_GET['ort_id']}' and ort_id=event_ort_id";
  if(!$res=ShopDB::query($query)){
    return 0;
  }
  while($name=mysql_fetch_array($res)){
    if(!$used){
      echo "<div class=error>".in_use."</div>";
    }
    echo "<div class=error>".$name['event_name']."</div>";
    $used=TRUE;
  }
  if($used){
    return;
  }
  
  $ort=Ort::load($_GET['ort_id']);
  $ort->delete(); 
  $this->ort_list();
}else
if($_GET['action']=='import'){

  $this->shared_ort_list();
}else{

  $this->ort_list();
}
}
}
?>
